package com.adams.custom.page.core.dao.impl;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.adams.access.db.bean.DicDatabase;
import com.adams.access.db.dao.hibernate.HibernateGenericDao;
import com.adams.core.exception.BusinessException;
import com.adams.custom.page.util.ViewCacheUtil;

/**
 * 
 * File Name : DicDatabaseDaoImpl.java
 * 
 * @Description : <Detail Description for Class >
 */
public class DicDatabaseDaoImpl extends
		HibernateGenericDao<DicDatabase, String>
{
	Logger logger = Logger.getLogger(DicDatabaseDaoImpl.class.getName());

	private static String cacheMap_key = "DicDatabase_cacheMap";

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.adams.access.db.dao.hibernate.HibernateGenericDao#save(java
	 * .lang.Object)
	 */
	// public void save(DicDatabase model)
	// {
	// super.save(model);
	// }
	/**
	 * Description : 根据tablecode获得表定义
	 * 
	 * @param tablecode
	 * @return
	 * @throws BusinessException
	 * 
	 */
	@SuppressWarnings("unchecked")
	public DicDatabase getDicDatabase(String databasecode)
			throws BusinessException
	{
		Map cacheMap = ViewCacheUtil.getConfigDataCacheMap(cacheMap_key);
		if (!cacheMap.containsKey(databasecode))
		{
			synchronizationCache();
		}
		DicDatabase result = (DicDatabase) cacheMap.get(databasecode);
		if (null == result)
		{
			logger.error(databasecode + " 数据库定义不存在！");
			throw new BusinessException("database.config.notexists.exception");
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.adams.access.db.dao.hibernate.HibernateGenericDao#delete(java
	 * .lang.Object)
	 */
	// public void delete(DicDatabase model)
	// {
	// super.delete(model);
	// }
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.adams.access.db.dao.hibernate.HibernateGenericDao#saveOrUpdate
	 * (java.lang.Object)
	 */
	// public void saveOrUpdate(DicDatabase model)
	// {
	// super.saveOrUpdate(model);
	// }
	/**
	 * Description : 得到所有的数据库定义对象
	 * 
	 * @return
	 * 
	 */
	// @SuppressWarnings("unchecked")
	// public List<DicDatabase> findAll()
	// {
	// List<DicDatabase> result = null;
	// Map cacheMap = ViewCacheUtil.getCacheMap(cacheMap_key);
	// if (cacheMap.size() == 0)
	// {
	// result = synchronizationCache();
	// return result;
	// } else
	// {
	// result = new ArrayList<DicDatabase>();
	// Set keySet = cacheMap.keySet();
	// for (Iterator it = keySet.iterator(); it.hasNext();)
	// {
	// Object key = it.next();
	// DicDatabase temp = (DicDatabase) cacheMap.get(key);
	// if (null != temp)
	// {
	// result.add(temp);
	// }
	// }
	// }
	// return result;
	// }
	private List<DicDatabase> findAllFromDB()
	{
		List<DicDatabase> result = query("from DicDatabase");
		return result;
	}

	/**
	 * Description : 同步缓存
	 * 
	 * @return
	 * 
	 */
	@SuppressWarnings("unchecked")
	public synchronized List<DicDatabase> synchronizationCache()
	{
		List<DicDatabase> result = findAllFromDB();
		Map cacheMap = ViewCacheUtil.getConfigDataCacheMap(cacheMap_key);
		for (DicDatabase dicDatabase : result)
		{
			cacheMap.put(dicDatabase.getDatabasecode(), dicDatabase);
		}
		ViewCacheUtil.put2ConfigDataCacheMap(cacheMap_key, cacheMap);
		return result;
	}
}
